package com.jtlrm.ckd.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.widget.TitleBar;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.BaseActivity;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.event.RemarkUpdate;
import com.jtlrm.ckd.mvp.model.HuanZheGuanLiModel;
import com.jtlrm.ckd.util.EventBusUtil;

import butterknife.BindView;

public class RemarkActivity extends TitleBarActivity {
    @BindView(R.id.remark)
    EditText remarkE;
    boolean isEdite = true;
    String remark;
    String userId;
    HuanZheGuanLiModel huanZheGuanLiModel;

    public static void goAcitvity(Context context, String userId, boolean isEdite, String remark) {
        Intent intent = new Intent(context, RemarkActivity.class);
        intent.putExtra("userId", userId);
        intent.putExtra("isEdit", isEdite);
        intent.putExtra("remark", remark);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        isEdite = intent.getBooleanExtra("isEdit", true);
        remark = intent.getStringExtra("remark");
        userId = intent.getStringExtra("userId");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        if (isEdite) {
            huanZheGuanLiModel = new HuanZheGuanLiModel();
            titleBar.tvRight.setText("保存");
            titleBar.tvRight.setVisibility(View.VISIBLE);
            remarkE.setEnabled(true);
            titleBar.tvRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    save();
                }
            });
            return "修改备注";
        } else {
            titleBar.tvRight.setVisibility(View.VISIBLE);
            remarkE.setEnabled(false);
            return "备注";
        }
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_remark);
    }

    @Override
    protected void initView() {
        setText(remarkE, remark);
    }

    /**
     * 保存备注
     */
    private void save() {
        showLoadingDialog("保存中");
        huanZheGuanLiModel.changeRemark(userId, remarkE.getText() + "", new CommonObserver<Object>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(Object data) {
                dismissLoadingDialog();
                RemarkUpdate update = new RemarkUpdate();
                update.setUserId(userId);
                EventBusUtil.postEvent(update);
                showToast("保存成功");
                finish();
            }
        }, lifecycleSubject);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

    }
}
